package com.bt.lms.ui.page.sys.study;

import com.bt.lms.constants.PageConstant;
import com.bt.lms.entity.page.Page;
import com.bt.lms.ui.page.common.PageLocation;
import com.bt.lms.ui.page.sys.utils.*;
import com.bt.lms.ui.vo.PageSkipInfo;
import com.bt.lms.main.BotLmsApp;
import com.bt.lms.ui.page.sys.study.action.SASaveBtn;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

/**
 * 添加学习中心界面
 *
 * @author 徐大永
 */
public class PageAdd extends VBox {

    /**
     * 学习中心名称
     */
    public static final String NAME_FT = "nameTF";

    /**
     * 地址一级
     */
    public static final String LOCATION_ONE = "locationOne";

    /**
     * 地址二级
     */
    public static final String LOCATION_TWO = "locationTwo";

    /**
     * 地址三级
     */
    public static final String LOCATION_THREE = "locationThree";

    /**
     * 联系人
     */
    public static final String RELATION_TF = "relationPenTF";

    /**
     * 移动电话
     */
    public static final String PHONE_TF = "phoneTF";

    /**
     * 电子邮箱
     */
    public static final String EMAIL_TF = "emailTF";

    /**
     * 学习中心描述
     */
    public static final String DESCRIBE_TA = "describeTA";


    /**
     * 界面跳转信息
     */
    private final PageSkipInfo pageSkipInfo;

    /**
     * 构建界面
     *
     * @param pageSkipInfo 界面跳转信息
     */
    public PageAdd(PageSkipInfo pageSkipInfo) {

        //维护
        this.pageSkipInfo = pageSkipInfo;

        //大小
        this.setPrefWidth(BotLmsApp.PER_WIGHT);
        this.setPrefHeight(BotLmsApp.HEIGHT);

        //组件
        //定位区域
        MyHBox locationHBox = new MyHBox();
        PageLocation pageLocation = new PageLocation("系统管理", "系统管理", "新增学习中心");
        locationHBox.getChildren().add(pageLocation);
        this.getChildren().add(locationHBox);

        //输入区域
        MyVBox inputVBox = new MyVBox();
        inputVBoxInit(inputVBox);
        ObservableList<Node> inputChildren = this.getChildren();
        inputChildren.add(inputVBox);

        //控制区域
        MyHBox controlHBox = new MyHBox();
        controlHBoxInit(controlHBox);
        ObservableList<Node> controlChildren = this.getChildren();
        controlChildren.add(controlHBox);
    }

    /**
     * 输入区域初始化
     *
     * @param inputVBox 输入区域
     */
    private void inputVBoxInit(MyVBox inputVBox) {

        //布局
        Insets insets = new Insets(PageConstant.PANE_TINY_PADDING);
        inputVBox.setPadding(insets);
        inputVBox.setSpacing(PageConstant.PANE_TINY_SPACING);
        inputVBox.setAlignment(Pos.CENTER_LEFT);

        //组件
        Label locationLab = new Label("新增学习中心");
        //组件添加
        ObservableList<Node> locationChildren = inputVBox.getChildren();
        locationChildren.add(locationLab);

        //组件（输入网格
        GridPane gridPane = inputGrid();
        inputVBox.getChildren().add(gridPane);
    }

    /**
     * 网格布局
     *
     * @return 网格布局
     */
    private GridPane inputGrid() {

        //新建网格布局
        GridPane res = new GridPane();

        //大小
        res.setPrefWidth(BotLmsApp.PER_WIGHT);
        res.setPadding(new Insets(PageConstant.GRID_PANE_PADDING));
        res.setHgap(PageConstant.GRID_PANE_H);//间隙（左右）
        res.setVgap(PageConstant.GRID_PANE_V);//间隙（上下）

        //学习中心名称 coding
        Label nameLab = new Label("学习中心名称：");
        TextField nameTF = new TextField();
        nameTF.setId(NAME_FT);
        res.add(nameLab, 0, 1);
        res.add(nameTF, 1, 1);
        GridPane.setHalignment(nameLab, HPos.RIGHT);
        Label codingMustLab = new Label("*");
        codingMustLab.setTextFill(Color.RED);
        res.add(codingMustLab, 2, 1);

        //学习中心编码 coding
        Label codingLab = new Label("学习中心编码：");
        Label coding = new Label("保存成功后即可生成。");
        res.add(codingLab, 0, 2);
        res.add(coding, 1, 2);
        GridPane.setHalignment(codingLab, HPos.RIGHT);

        //所在地址
        Label label = new Label("所在地：");
        res.add(label, 0, 3);
        GridPane.setHalignment(label, HPos.RIGHT);
        MyHBox regionHBox = new MyHBox();
        regionHBox.setSpacing(10);
        res.add(regionHBox, 1, 3);

        ComboBox<String> oneRegion = new ComboBox<>();
        oneRegion.setId(LOCATION_ONE);
        ComboBox<String> twoRegion = new ComboBox<>();
        twoRegion.setId(LOCATION_TWO);
        ComboBox<String> threeRegion = new ComboBox<>();
        threeRegion.setId(LOCATION_THREE);
        regionHBox.getChildren().addAll(oneRegion, twoRegion, threeRegion);

        //联动准备
        oneRegion.getItems().addAll("省");
        oneRegion.getSelectionModel().selectFirst();
        twoRegion.getItems().addAll("市");
        twoRegion.getSelectionModel().selectFirst();
        threeRegion.getItems().addAll("区/街道");
        threeRegion.getSelectionModel().selectFirst();
        //联动
        AddressUtils.ganged(oneRegion, twoRegion, threeRegion);


        //联系人 relation
        Label relationPenLab = new Label("联系人：");
        TextField relationPenTF = new TextField();
        relationPenTF.setId(RELATION_TF);
        res.add(relationPenLab, 0, 4);
        res.add(relationPenTF, 1, 4);
        GridPane.setHalignment(relationPenLab, HPos.RIGHT);

        //移动电话 phone
        Label phoneLab = new Label("移动电话：");
        TextField phonePenTF = new TextField();
        phonePenTF.setId(PHONE_TF);
        res.add(phoneLab, 0, 5);
        res.add(phonePenTF, 1, 5);
        GridPane.setHalignment(phoneLab, HPos.RIGHT);

        //电子邮箱 email
        Label emailLab = new Label("电子邮箱：");
        TextField emailTF = new TextField();
        emailTF.setId(EMAIL_TF);
        res.add(emailLab, 0, 6);
        res.add(emailTF, 1, 6);
        GridPane.setHalignment(emailLab, HPos.RIGHT);

        //学习中心描述 describe
        Label describeLab = new Label("学习中心描述：");
        TextArea describeTA = new TextArea();
        describeTA.setPrefHeight(PageConstant.NODE_GENERAL_HEIGHT);
        describeTA.setId(DESCRIBE_TA);
        res.add(describeLab, 0, 7);
        res.add(describeTA, 1, 7);
        GridPane.setHalignment(describeLab, HPos.RIGHT);

        return res;
    }

    /**
     * 控制区域初始化
     *
     * @param controlHBox 控制区域
     */
    private void controlHBoxInit(MyHBox controlHBox) {

        //布局
        Insets insets = new Insets(10);
        controlHBox.setPadding(insets);
        controlHBox.setSpacing(10);
        controlHBox.setAlignment(Pos.CENTER);

        //组件(保存)
        MyButton saveBtn = new MyButton("保存");
        //大小控制
        Insets saveBtnInsets = new Insets(10, 20, 10, 20);
        saveBtn.setPadding(saveBtnInsets);
        //组件添加
        ObservableList<Node> saveBtnChildren = controlHBox.getChildren();
        saveBtnChildren.add(saveBtn);
        //事件
        SASaveBtn SASaveBtnAction = new SASaveBtn(pageSkipInfo);
        saveBtn.setOnAction(SASaveBtnAction);

        //组件(取消)
        MyButton cancelBtn = new MyButton("取消");
        //大小控制
        Insets cancelBtnInsets = new Insets(10, 20, 10, 20);
        cancelBtn.setPadding(cancelBtnInsets);
        //组件添加
        ObservableList<Node> cancelBtnChildren = controlHBox.getChildren();
        cancelBtnChildren.add(cancelBtn);
        //事件
        cancelBtn.setOnAction(event -> NetWorkUtils.reqStudyCenPagingPage(pageSkipInfo, new Page<>()));

    }

}